package mainpk;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordListener;
import javax.microedition.rms.RecordStore;

public class ListAlunos extends List implements CommandListener, RecordListener {
	
	private Animation midlet;
	private Command cmExit, cmNew, cmDelete;

	public ListAlunos(Animation parent, String title, String[] stringElements) {
		super(title, IMPLICIT, stringElements, null);
		this.midlet = parent;
		
		this.cmExit   = new Command("Exit", Command.EXIT, 0);
		this.cmNew    = new Command("New", Command.ITEM, 0);
		this.cmDelete = new Command("Delete", Command.ITEM, 0);
		
		this.addCommand(cmExit);
		this.addCommand(cmNew);
		this.addCommand(cmDelete);
		
		this.setCommandListener(this);
		
	}

	public void commandAction(Command c, Displayable d) {
		if (c == cmExit) {
			try {
				this.midlet.destroyApp(true);
			} catch (MIDletStateChangeException e) {
				e.printStackTrace();
			}
			midlet.notifyDestroyed();
		}
		if(c == cmNew) {
			this.midlet.fmAluno.setMode(FormAluno.NEW, 0);
			this.midlet.displayMgr.pushDisplayable(this.midlet.fmAluno);
		}
		
		if(c == cmDelete) {
			int index = this.getSelectedIndex();
			int id = this.midlet.disc.getIdFromIndex(index);
			this.midlet.disc.delete(id);
			this.delete(index);
		}

	}

	public void recordAdded(RecordStore rs, int id) {
		Aluno a = new Aluno();
		this.midlet.disc.read(id, a);
		this.append(a.getName(), null);
	}

	public void recordChanged(RecordStore rs, int id) {

	}

	public void recordDeleted(RecordStore rs, int id) {

	}
}
